package com.cheetah.start.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.module.system.domain.ScheduleLog;
import com.cheetah.start.module.system.service.IScheduleLogService;
import com.cheetah.start.module.system.mapper.ScheduleLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Describe: 定时任务日志服务
 * Author: 就免仪式
 * CreateTime: 2019/10/23
 */
@Slf4j
@Service("scheduleLogService")
public class ScheduleLogServiceImpl extends ServiceImpl<ScheduleLogMapper, ScheduleLog> implements IScheduleLogService {

    @Resource
    private ScheduleLogMapper scheduleLogMapper;

    /**
     * Describe: 定时任务日志入库
     * Param: ScheduleJob
     * Return: Boolean 执行结果
     */
    @Override
    public Boolean insert(ScheduleLog scheduleLogBean) {
        int i = scheduleLogMapper.insert(scheduleLogBean);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public Page<ScheduleLog> pageList(ScheduleLog scheduleLogBean, PageQuery pageDomain) {
        Page<ScheduleLog> pageInfo = new Page<>(pageDomain.getPage(), pageDomain.getLimit());
        LambdaQueryWrapper<ScheduleLog> queryW = getQueryW(scheduleLogBean);
        page(pageInfo, queryW);
        return pageInfo;
    }


    private LambdaQueryWrapper<ScheduleLog> getQueryW(ScheduleLog scheduleLog) {
        LambdaQueryWrapper<ScheduleLog> queryWrapper = Wrappers.lambdaQuery(ScheduleLog.class);
        queryWrapper.orderByDesc(ScheduleLog::getCreateTime);
        if (scheduleLog != null) {
            String jobName = scheduleLog.getJobName();
            if (StrUtil.isNotBlank(jobName)) {
                queryWrapper.like(ScheduleLog::getJobName, jobName);
            }
        }
        return queryWrapper;
    }
}
